package de.gwtprime.server.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import de.gwtprime.server.model.User2;
import de.gwtprime.server.sql.helper.HibernateHelper;
import de.gwtprime.shared.util.AppUtilsShared;
import de.gwtprime.util.Helper;

/**
 * If a user subscribes, he gets an email with an activation email.
 * The link directs to this servlet. Email and hash are validated and the user
 * is activated.
 */
public class ActivationServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Logger
	 */
	private static Logger logger = Logger.getLogger(ActivationServlet.class);

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {


		PrintWriter writer = resp.getWriter();
		writer.println("<html>");
		writer.println("<head>");
		writer.println("<link type=\"text/css\" rel=\"stylesheet\" href=\"servletCSS.css\"");
		writer.println("</head>");
		writer.println("<body>");
		writer.println("<div class=\"servletMessage\">");

		String email = req.getParameter("email");
		String hash = req.getParameter("hash");

		//hash = "3923271#6010295353";

		if(Helper.isNullOrEmpty(email) || Helper.isNullOrEmpty(hash)){
			logger.error("Activation failed. 'email' or 'hash'-parameter are empty");

			writer.println("Activation failed! The paramters of the link are not present.");
			return;
		}
		try{
			User2 u = HibernateHelper.queryDB(new User2(), String.format("from User2 where email = '%s' AND activationCode = '%s'", email, hash));

			if(u!=null){


				if(u.isActive()){
					logger.info("User "+email+" is already activated.");
					writer.println("User <b>"+email+"</b> is already activated.");
					return;
				}

				u.setActive(true);

				u.persist();
				//entityManager.getTransaction().begin();

				logger.info("User "+email+" was successfully activated.");
				writer.println("User "+email+" was successfully activated. Weiter zu <a href='"+AppUtilsShared.DOMAIN_PROD+"'>Litecoin Börse</a>");

			}else{
				logger.warn("There is no user with email: "+email+" and hash: "+hash);
				writer.println("There is no user with email: "+email+" and hash: "+hash);
				return;
			}
		}catch(Exception e){
			logger.error(e);
			writer.println(e);
		}finally{
			writer.println("</div>");
			writer.println("</body>");
			writer.println("</html>");
		}
	}

	/**
	 * Create an link to the servlet with email and activation hash.
	 * If the user clicks the link, his account will be activated.
	 * @param recipient
	 * @param activationCode
	 * @return
	 */
	public static String generateActivationLink(String recipient, String
			activationCode){

		return AppUtilsShared.DOMAIN_PROD +
				"activate?email="+recipient+"&hash="+activationCode;

	}
}
